package cn.gson.crm.model.service;

import cn.gson.crm.model.mapper.RefundMapper;
import cn.gson.crm.model.mapper.RefundRecordMapper;
import cn.gson.crm.model.mapper.UserMapper;
import cn.gson.crm.model.pojos.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class RefundService {
    @Autowired
    RefundMapper refundMapper;
    @Autowired
    RefundRecordMapper refundRecordMapper;
    @Autowired
    UserMapper userMapper;
    //通用方法查询当前登录人是否为老板
    public Integer fouyuan(HttpSession session){
        User login = (User)session.getAttribute("login");
        int ygId=login.getyId();
        //查询是否为老板
        List<User> userList=userMapper.selectctlaofou(ygId);
        List<Role> roleList=userList.get(0).getRoleList();
        for(int i=0;i<roleList.size();i++){
            if(roleList.get(i).getjZil()==1){
                return null;
            }
        }
        int ygId2= (int) login.getEmployees().get(0).getYgId();
        return ygId2;
    }
    //查询所有
    public Map<String,Object> selectref(HttpSession session,Integer rid,Integer page, Integer size,String jinnian,String qunian,String qiannian){
        rid=fouyuan(session);
        List<Refund> refundList2=refundMapper.selectref(rid);
        Page<Object> obj = PageHelper.startPage(page, size);
        List<Refund> refundList=refundMapper.selectref(rid);
        Map<String,Object> map = new HashMap<>();
        List<Refund> contractList4=refundMapper.selectnian(rid,jinnian);
        List<Refund> contractList5=refundMapper.selectnian(rid,qunian);
        List<Refund> contractList6=refundMapper.selectnian(rid,qiannian);
        User login = (User)session.getAttribute("login");
        int ygId=login.getyId();
        List<Customer> customerList=refundRecordMapper.selectcustomer(ygId);
        map.put("total",obj.getTotal());
        map.put("rows",refundList);
        map.put("jinnian",contractList4);
        map.put("qunian",contractList5);
        map.put("qiannian",contractList6);
        map.put("customerList",customerList);
        map.put("tong",refundList2);
        return map;
    };
    //时间查询
    public Map<String,Object> selecttiem(HttpSession session,Integer rid, String statr, String endtime,Integer page, Integer size){
        rid=fouyuan(session);
        Page<Object> obj = PageHelper.startPage(page, size);
        List<Refund> refundList=refundMapper.selecttiem(rid,statr,endtime);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",refundList);
        return map;
    };
    //条件查询
    public Map<String,Object> selectwhere(HttpSession session,Integer rid,String where,Integer page, Integer size){
        rid=fouyuan(session);
        Page<Object> obj = PageHelper.startPage(page, size);
        List<Refund> refundList=refundMapper.selectwhere(rid,where);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",refundList);
        return map;
    };
}
